﻿using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace AspnetUtil.Web.Filter.Auth
{
    /// <summary>
    /// 用户缓存
    /// </summary>
    public static class UserCache
    {
        private static readonly Dictionary<string, UserInfo> UserCacheDict = new();

        /// <summary>
        /// 设置用户信息
        /// </summary>
        /// <param name="info"></param>
        public static void SetUser(UserInfo info)
        {
            UserCacheDict[GetCacheKey(info.UserId)] = info;
        }

        /// <summary>
        ///  获取缓存用户信息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public static UserInfo GetUser(long uid)
        {
            return UserCacheDict[GetCacheKey(uid)];
        }

        /// <summary>
        /// 删除用户缓存
        /// </summary>
        /// <param name="uid"></param>
        public static void DeleteUser(long uid)
        {
            UserCacheDict.Remove(GetCacheKey(uid));
        }

        /// <summary>
        /// 获取缓存的 key
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        private static string GetCacheKey(long uid)
        {
            return "u-" + uid;
        }
    }
}